/**
 * @author : xwj
 * @date : 2021/7/20 17:08
 * @description : 通常情况下，罗马数字中小的数字在大的数字的右边。
 * 但也存在特例，例如 4 不写做 IIII，而是 IV。
 * 数字 1 在数字 5 的左边，所表示的数等于大数 5 减小数 1 得到的数值 4 。
 * 同样地，数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况：
 *
 * I 可以放在 V (5) 和 X (10) 的左边，来表示 4 和 9。
 * X 可以放在 L (50) 和 C (100) 的左边，来表示 40 和 90。 
 * C 可以放在 D (500) 和 M (1000) 的左边，来表示 400 和 900。
 * 给你一个整数，将其转为罗马数字。
 *
 * 字符          数值
 * I             1
 * V             5
 * X             10
 * L             50
 * C             100
 * D             500
 * M             1000
 * @class : test12
 **/

public class test12 {

    public static String intToRoman(int num) {
        StringBuilder str = new StringBuilder(20);
        int flag = 0;
        while (num != 0){
            int a;
            switch (flag){
                case 0:
                    flag++;
                    a = num / 1000;
                    num = num - a*1000;
                    switch (a){
                        case 1:
                            str.append("M");
                            continue;
                        case 2:
                            str.append("MM");
                            continue;
                        case 3:
                            str.append("MMM");
                            continue;
                        case 0:
                            continue;
                    }
                    continue;
                case 1:
                    flag++;
                    a = num / 100;
                    num = num - a*100;
                    switch (a){
                        case 1:
                            str.append('C');
                            continue;
                        case 2:
                            str.append("CC");
                            continue;
                        case 3:
                            str.append("CCC");
                            continue;
                        case 4:
                            str.append("CD");
                            continue;
                        case 5:
                            str.append("D");
                            continue;
                        case 6:
                            str.append("DC");
                            continue;
                        case 7:
                            str.append("DCC");
                            continue;
                        case 8:
                            str.append("DCCC");
                            continue;
                        case 9:
                            str.append("CM");
                            continue;
                        case 0:
                            continue;
                    }
                    continue;
                case 2:
                    flag++;
                    a = num / 10;
                    num = num - a*10;
                    switch (a){
                        case 1:
                            str.append('X');
                            continue;
                        case 2:
                            str.append("XX");
                            continue;
                        case 3:
                            str.append("XXX");
                            continue;
                        case 4:
                            str.append("XL");
                            continue;
                        case 5:
                            str.append("L");
                            continue;
                        case 6:
                            str.append("LX");
                            continue;
                        case 7:
                            str.append("LXX");
                            continue;
                        case 8:
                            str.append("LXXX");
                            continue;
                        case 9:
                            str.append("XC");
                            continue;
                        case 0:
                            continue;
                    }
                    continue;
                case 3:
                    flag++;
                    a = num;
                    num = 0;
                    switch (a){
                        case 1:
                            str.append('I');
                            continue;
                        case 2:
                            str.append("II");
                            continue;
                        case 3:
                            str.append("III");
                            continue;
                        case 4:
                            str.append("IV");
                            continue;
                        case 5:
                            str.append("V");
                            continue;
                        case 6:
                            str.append("VI");
                            continue;
                        case 7:
                            str.append("VII");
                            continue;
                        case 8:
                            str.append("VIII");
                            continue;
                        case 9:
                            str.append("IX");
                            continue;
                        case 0:
                            continue;
                    }
                    continue;
            }
        }
        return String.valueOf(str);
    }

    public static String intToRoman1(int num) {
        String[] I = {"","I","II","III","IV","V","VI","VII","VIII","IX"};
        String[] X = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
        String[] C = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
        String[] M = {"","M","MM","MMM"};
        return M[num/1000]+C[(num%1000)/100]+X[(num%100)/10]+I[(num%10)];
    }

    public static void main(String[] args) {
        int n = 1999;
        System.out.println(intToRoman1(n));
    }

}
